package pl.mmirski.functests.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class ProductBacklogPage extends Page {

	public ProductBacklogPage(WebDriver driver) {
		super(driver);
	}

	public void createNewItem(String userStory, String description, String tag) throws InterruptedException {
		clickOnElement(By.linkText("Add item"));
		
		waitForElement(By.id("item_user_story"));	
		inputText(By.id("item_user_story"), userStory);
		
		Thread.sleep(10000);
		
		waitForElement(By.id("item_description"));
		inputText(By.id("item_description"), description);			
		selectValueFromDropDown(By.id("item_estimate"), "3");
		inputText(By.id("new_item_tag"), tag);
		clickOnElement(By.xpath("//input[@name='commit'][@value='Create']"));
		
	}

	public boolean isItemCreated(String userStory, String description,
			String tag) {
		
		waitForElementOnTheList(By.className("item-user-story"), userStory);
		
		String id = getElementIdByText(By.className("backlog-item"), userStory);
		
		clickOnElement(By.xpath("//li[@id='" + id + "']/*/a[@class='expandable-link']"));
		
		boolean result = isTextPresent(
				By.xpath("//li[@id='" + id + "']/*/div[@class='item-user-story highlight']"), userStory);
		
		result = result && isTextPresent(
				By.xpath("//li[@id='" + id + "']/*/div[@class='item-description']/*/div[@class='text-wrapper']/p"), 
				description);
		
		result = result && isElementPresentOnTheListByText(
				By.xpath("//li[@id='" + id + "']/*/div[@class='controls']/div[@class='tags-for-item']/descendant::span[@class='tag-name']"), 
				tag); 

		return result;
	}
	



}
